/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package broadcast;

import common.function_common;
import common.global;
import java.io.IOException;
import java.net.*;
import myPackage.*;

/**
 *
 * @author Chickyky
 */
public class myBroastcastSender {

    byte[] bufferSender;
    String msgSender;
    InetAddress desAddress;
    int desPort;
    MulticastSocket senderSocket;
    DatagramPacket byteSender;
    String ipGroup = global.LANgroup;
    
    public myBroastcastSender(MulticastSocket ms, InetAddress inetAddress, int port)
    {
        this.senderSocket = ms;
        this.desAddress = inetAddress;
        this.desPort = port;
    }


    public void sendBroadcast(myPackage.Package pk) throws IOException {
        // Determine the actual size of the message
        byte[] buffer = function_common.serialize(pk);
        try {
            // Create the packet of information
            DatagramPacket infoPacket =
                    new DatagramPacket(buffer, buffer.length, desAddress, this.desPort);
            // Send the packet and clean up the resources
            senderSocket.send(infoPacket);
            senderSocket.setSoTimeout(5000);
//            senderSocket.close();
        } catch (Exception ex) {
            System.out.println("Problem creating socket on port: " + desPort);
            System.exit(-1);
        }
    }
}
